package cn.com.czpb.client.module.subscribe.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import cn.com.czpb.client.R;
import cn.com.czpb.client.api.InterfaceJsonfile;
import cn.com.czpb.client.app.MToolBarActivity;
import cn.com.czpb.client.model.base.BaseEntity;
import cn.com.czpb.client.model.event.PublishEventForPublisher;
import cn.com.czpb.client.model.subscribe.SubscribeListBean;
import cn.com.czpb.client.model.subscribe.SubscribeListEntity;
import cn.com.czpb.client.model.subscribe.SubscribeListSecondBean;
import cn.com.czpb.client.model.subscribe.SubscribeListSecondEntity;
import cn.com.czpb.client.module.subscribe.adapter.SubscribeMoreAdapter;
import cn.com.czpb.client.module.subscribe.adapter.SubscribeMoreListAdapter;
import cn.com.czpb.client.net.Factory;
import cn.com.czpb.client.utils.DeviceUtils;
import cn.com.czpb.client.utils.LogUtils;
import cn.com.czpb.client.utils.PageCtrl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by 猪猪侠 on 2017/7/28.
 */

public class SubscribeMoreActivity extends MToolBarActivity implements  BaseQuickAdapter.OnItemChildClickListener, BaseQuickAdapter.RequestLoadMoreListener {
    @BindView(R.id.recycler_list_id)
    RecyclerView recyclerListId;
    @BindView(R.id.recycler_list2_id)
    RecyclerView recyclerList2Id;
    @BindView(R.id.ll_back)
    LinearLayout ll_back;
    @BindView(R.id.layout_title)
    TextView tv_title;


    private SubscribeMoreAdapter adapter;
    private SubscribeMoreListAdapter adapter2;
    private int page = 1;
    private int page2 = 1;
    private static final int pageSize = 15;//
    private String currentid;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_subscribe_more;
    }
    @Override
    public void initView() {
        ll_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });

        //初始化swip
        tv_title.setText("添加您感兴趣的订阅号");
        final LinearLayoutManager layoutManager = new LinearLayoutManager(activity);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerListId.setLayoutManager(layoutManager);
        final LinearLayoutManager layoutManager2 = new LinearLayoutManager(activity);
        layoutManager2.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerList2Id.setLayoutManager(layoutManager2);
        initAdapter();
        initAdapter2();
    }
    /**
     * adapter init
     */
    private void initAdapter() {
        adapter = new SubscribeMoreAdapter(null);
        adapter.setOnItemChildClickListener(this);
//        adapter.setOnLoadMoreListener(this, recyclerListId);
        recyclerListId.setAdapter(adapter);
    }
    @Override
    public void initData() {
        getSubscribeList();
    }
    /**
     * 订阅栏目分类列表
     */
    private void getSubscribeList() {
        Map<String, String> map = new HashMap();
        map.put("siteid", InterfaceJsonfile.SITEID);
        Factory.provideHttpService().subscribeList(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<SubscribeListEntity>() {
                    @Override
                    public void call(SubscribeListEntity entity) {
                        setListData(entity.data);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        adapter.loadMoreFail();
                    }
                });
    }

    public void setListData(List<SubscribeListBean> list) {
        adapter.addData(list);
        adapter.notifyDataSetChanged();
        adapter.loadMoreComplete();
        LogUtils.i("size-->" + adapter.getData().size());
        if (page==1){
            currentid = list.get(0).getId();
            getVideoListData2(list.get(0).getId());
        }
        if (list.size() < pageSize) {
            //关闭刷新
            adapter.loadMoreEnd(true);
        }
    }
    @Override
    public void onItemChildClick(BaseQuickAdapter bsadapter, View view, int position) {
        SubscribeListBean listBean = (SubscribeListBean) bsadapter.getItem(position);
        switch (view.getId()) {
            case R.id.rl_root_id:

                currentid = listBean.getId();
                adapter.setPosition(position);
                page2=1;
                getVideoListData2(listBean.getId());
                break;
        }
    }

    private void initAdapter2() {
        adapter2 = new SubscribeMoreListAdapter(null);
        adapter2.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                SubscribeListSecondBean bean = (SubscribeListSecondBean) adapter.getItem(position);
                switch (view.getId()){
                    case R.id.img_subscribe_id:
                        if (spu.getUser() != null) {
                            //订阅
                            LogUtils.i("订阅");
                            focusUser(bean.getUid());
                        } else {
                            PageCtrl.start2LoginActivity(activity);
                        }

                        break;
                    case R.id.img_remove_subscribe_id:

                        if (spu.getUser() != null) {
                            //订阅
                            LogUtils.i("取消订阅");
                            removeFocusUser(bean.getUid());
                        } else {
                            PageCtrl.start2LoginActivity(activity);
                        }

                        break;
                    case R.id.ll_root_id:
                        //作者个人主页
                        PageCtrl.start2SubscribeDetailActivity(activity,bean.getUid(),bean.getTid());
                        break;
                }
            }
        });
        recyclerList2Id.setAdapter(adapter2);
    }
    //订阅达人号
    private void focusUser(String uid){
        Map<String ,String> map = new HashMap<>();
        if (spu.getUser()!=null){
            map.put("uid",spu.getUser().getUid());
        }
        map.put("device",DeviceUtils.getMyUUID(activity));
        map.put("focusid",uid);
        final String tuid = uid;
        Factory.provideHttpService().focusUser(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<BaseEntity>() {
                    @Override
                    public void call(BaseEntity baseEntity) {
                        PublishEventForPublisher event = new PublishEventForPublisher();
                        event.setUid(tuid);
                        event.setPublished(1);
                        EventBus.getDefault().post(event);
                        getVideoListData2(currentid);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {

                    }
                });

    }
    //取消订阅
    private void removeFocusUser(String uid){
        Map<String ,String> map = new HashMap<>();
        if (spu.getUser()!=null){
            map.put("uid",spu.getUser().getUid());
        }
        map.put("device",DeviceUtils.getMyUUID(activity));
        map.put("focusid",uid);
        final String tuid = uid;
        Factory.provideHttpService().removeFocusUser(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<BaseEntity>() {
                    @Override
                    public void call(BaseEntity baseEntity) {
                        PublishEventForPublisher event = new PublishEventForPublisher();
                        event.setUid(tuid);
                        event.setPublished(0);
                        EventBus.getDefault().post(event);
                        getVideoListData2(currentid);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {

                    }
                });

    }
    /**
     * 获取视频列表数据
     */
    private void getVideoListData2(String id) {
        Map<String, String> map = new HashMap();
        map.put("siteid", InterfaceJsonfile.SITEID);
        map.put("id", id);
        if (spu.getUser()!=null){
            map.put("uid",spu.getUser().getUid());
        }
        map.put("device", DeviceUtils.getMyUUID(activity));
        map.put("Page", page2 + "");
        map.put("PageSize", pageSize + "");
        Factory.provideHttpService().subscribeSecondList(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<SubscribeListSecondEntity>() {
                    @Override
                    public void call(SubscribeListSecondEntity entity) {
                        setListData2(entity.data);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        adapter2.loadMoreFail();
                    }
                });
    }
    public void setListData2(List<SubscribeListSecondBean> list) {
        adapter2.setNewData(list);
    }
    private int mCurrentCounter = 0;
    private static final int TOTAL_COUNTER = 18;
    private boolean isErr;
    private boolean mLoadMoreEndGone = false;

    @Override
    public void onLoadMoreRequested() {
        page++;
        getSubscribeList();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


}
